module Syspec
  
  class Condition
    
    attr_accessor :operation, :expected, :actual
    
    def initialize(operation, expected)
      self.operation = operation
      self.expected = expected
    end
    
    def compare(actual)
      self.actual = actual and return result
    end
    
    def result
      @result ||= expected.send(operator, actual)
    end
    
    def operator
      @operator ||= case operation
      when :includes then :include?
      when :match then :=~
      when :equals then :==
      else raise StandardError, "unknown conditional operation"
      end
    end
    
    def expected_to_verbose
      case expected
      when Range then "between #{expected.first} and #{expected.last}"
      when Array then expected.join(" or ")
      else expected.inspect
      end
    end
    
    def operator_to_verbose
      case operator
      when :=~ then result ? "matches" : "does not match"
      else result ? "is" : "is not"
      end
    end

    def to_verbose
      "#{actual.inspect} #{operator_to_verbose} #{expected_to_verbose}"
    end

  end
      
end